<?php

/**
 * Description of guestCreateAction
 \* www.teuker.nl - mail@teuker.nl
 *
 * @author francisc
 */

class guestCreateAction extends sfAction {

	public function execute($request) {

		$this->form = new ReservationGuestForm();
		$params = $request->getParameter($this->form->getName());
		$params['date'] = Util::convertDateClientServer($params['date'], 'nl');
		$this->form->bind($params);
		$person_no=$this->form->getValue('person_no');
		if (empty($person_no)){
			$person_no=1;
		}
		$date=$params['date'];		 
		$time=$params['time_hour'].":".$params['time_minute'];
		$isValidTime=time()<=strtotime($date.' '.$time);
		$expired_msg=SqlEngine::getMsgForExpiredTimeReservation(array("person_no"=>$person_no,
					"restaurant_id"=>$this->getUser()->getAttribute('current_restaurant_id'),
					"date"=>$params['date'],"time"=>$time));

		if (!empty($expired_msg[0]['expired_msg'])||(!$isValidTime)){
			//$restaurant_id = $this->getUser()->getAttribute('current_restaurant_id');
			//$this->settings = SqlEngine::getSettings(array('restaurant_id' => $restaurant_id));
			$this->getUser()->setAttribute('expired_msg',$expired_msg[0]['expired_msg']);

			$this->redirect('@too_late_page');
		}
		else{

			if ($this->processGuestReservation($request, $this->form,$params)) {
				$token = md5(time());

				If ( $this->form->getValue('person_no') >= $this->getUser()->getAttribute('group_size')) {
					$resstatus = 2;
				} else {
					$resstatus = 1;
				}

				SqlEngineCore::get()->insert(
				array('table' => 'reservation',
                          'columns' => array(
                                'restaurant_id' => array('value' => $this->getUser()->getAttribute('current_restaurant_id'), 'type' => PDO::PARAM_INT),
				//                                'user_id' => $this->getUser()->getAttribute('user_id'),
                                'menu_id' => $this->form->getValue('menu_id'),
                                'created_at' => 'NOW()',
                                'date' => $this->form->getValue('date'),
                                'occasion' => $this->form->getValue('occasion'),
                                'time' => $this->form->getValue('time_hour') . ':' . $this->form->getValue('time_minute'),
                                'person_no' => array('value' => $this->form->getValue('person_no'), 'type' => PDO::PARAM_INT),
                                'salutation' => array('value' => $this->form->getValue('salutation'), 'type' => PDO::PARAM_INT),
                                'name_last' => $this->form->getValue('name_last'),
                                'name_first' => $this->form->getValue('name_first'),
                                'name_company' => $this->form->getValue('name_company'),
                                'address' => $this->form->getValue('address'),
                                'phone' => $this->form->getValue('phone'),
                                'mobile' => $this->form->getValue('mobile'),
                                'email' => $this->form->getValue('email'),
                                'comments_diet' => $this->form->getValue('comments_diet'),
                                'comments_general' => $this->form->getValue('comments_general'),
                    			'resstatus' => $resstatus,
                                'token' => $token)));

				/* remeber user options */
				$cookie_data = array('gender' => $this->form->getValue('salutation'),
                                 'name_last' => $this->form->getValue('name_last'),
                                 'name_first' => $this->form->getValue('name_first'),
                                 'name_company' => $this->form->getValue('name_company'),
                                 'address' => $this->form->getValue('address'),
                                 'phone' => $this->form->getValue('phone'),
                                 'mobile' => $this->form->getValue('mobile'),
                                 'email' => $this->form->getValue('email'),
            					 'comments_general'=> $this->form->getValue('comments_general'),
                                 'comments_diet' => $this->form->getValue('comments_diet'));


				$reservationId = SqlEngineCore::get()->getLastInsertId();
				$reservation = SqlEngine::getReservations(array('reservation_id' => $reservationId));
				Util::send_thank_you_email($reservation[0]);

				setcookie('guest_user_options', json_encode($cookie_data), time() + 60 * 60 * 24 * 365, '/');

				$this->redirect('@thankyou_page');
			}
			else {
				$this->setTemplate('guest');
			}

		}
	}

	protected function processGuestReservation(sfWebRequest $request, sfForm $form,$params) {

		$is_block_time = Util::is_block_time($params['date'],
		$params['time_hour'],
		$params['time_minute'],
		$this->getUser());

		$is_seat_limit = Util::is_seat_limit($params['person_no'],
		$params['date'],
		$params['time_hour'],
		$params['time_minute'],
		$this->getUser());

		if ($is_block_time['is_block_time']) {
			$request->setParameter('block_time', true);
			$request->setParameter('block_time_reason', $is_block_time['reason']);
		}
		else if ($is_seat_limit)
		$request->setParameter('seat_limit', true);

		return $form->isValid() && !$is_block_time['is_block_time'] && !$is_seat_limit;
	}
}